package cn.qy.kotlin.base

import java.nio.charset.Charset
import kotlin.test.Test

class StringTest {

    @Test
    fun fun1() {
        println("defaultCharset = ${Charset.defaultCharset()}")
        println("file.encoding = ${System.getProperty("file.encoding")}")
        println("你好，世界")

        val greeting = "你好, 世界"

        println("---------------")
        println(greeting) // 默认编码 默认编码
        println(String(greeting.toByteArray(), Charset.forName("gbk"))) // 默认编码 gbk
        println(String(greeting.toByteArray(), Charset.forName("utf-8"))) // 默认编码 utf-8
        println("---------------")

        println(String(greeting.toByteArray(charset("utf-8")))) // utf-8 默认编码
        println(String(greeting.toByteArray(charset("utf-8")), Charset.forName("gbk"))) // utf-8 gbk
        println(String(greeting.toByteArray(charset("utf-8")),  Charset.forName("utf-8"))) // utf-8 utf-8


        println("---------------")
        println(String(greeting.toByteArray(charset("gbk")))) // gbk 默认编码
        println(String(greeting.toByteArray(charset("gbk")), Charset.forName("gbk"))) // gbk gbk
        println(String(greeting.toByteArray(charset("gbk")),  Charset.forName("utf-8"))) // gbk utf-8
    }

}